package com.zm.demo.core.leetcode;

/**
 * 另一个树的子树
 * @author zoum
 * @create 2020/1/15 11:35
 */
public class IsSubtree {

    public boolean isSubtree(TreeNode s, TreeNode t) {

        if (s == null || t == null){
            return false;
        }

        while(s != null){
            if (s.val == t.val && isSubtree1(s.left, t.left) && isSubtree1(s.right, t.right)){
                return true;
            }else {
                return isSubtree(s.left, t) || isSubtree(s.right, t);
            }
        }
        return false;
    }

    public boolean isSubtree1(TreeNode s, TreeNode t) {
        if (s == null && t == null){
            return true;
        }else if ((s == null && t !=null) || (s != null && t ==null)){
            return false;
        }else if (s.val != t.val){
            return false;
        }else {
            return isSubtree1(s.left, t.left) && isSubtree1(s.right, t.right);
        }
    }
}
